//
//  CYLoanSectionView.m
//  CreditAddressBook
//
//  Created by LE on 15/11/5.
//  Copyright © 2015年 Lee. All rights reserved.
//

#import "CYLoanSectionView.h"
#import "UIView+Extension.h"

static NSString *SectionHeaderViewIdentifier = @"SectionHeaderViewIdentifier";

@interface CYLoanSectionView ()

@property (weak, nonatomic) IBOutlet UIView *bottomLine;
@end

@implementation CYLoanSectionView

+ (instancetype)sectionView {
    return [self loadFromNibUsingClassName];
}

+ (instancetype)headerFooterSectionView:(UITableView *)tableView {
    return [tableView dequeueReusableHeaderFooterViewWithIdentifier:SectionHeaderViewIdentifier];
}

+ (BOOL)registerHeaderFooterView:(UITableView *)tableView {
    UINib *sectionHeaderViewNib = [UINib nibWithNibName:@"CYLoanSectionView" bundle:nil];
    if (sectionHeaderViewNib) {
        [tableView registerNib:sectionHeaderViewNib forHeaderFooterViewReuseIdentifier:SectionHeaderViewIdentifier];
        return YES;
    }
    return NO;
}

- (void)awakeFromNib {
    self.cartBtn.selected = NO;
//    [self.cartBtn setImage:[UIImage imageNamed:@"carat-open"] forState:UIControlStateSelected];
    
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(toggleHeaderView)];
    [self addGestureRecognizer:tapGestureRecognizer];
}

- (void)toggleHeaderView {
    if(!self.cartBtn.selected) {
        if([self.delegate respondsToSelector:@selector(openSection:atPosition:)]) {
            [self.delegate openSection:self atPosition:self.section];
        }
    } else {
        if([self.delegate respondsToSelector:@selector(closeSection:atPosition:)]) {
            [self.delegate closeSection:self atPosition:self.section];
        }
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.bottomLine.height = 0.4;
}

@end
